/**
 * @param {*} name //cookie的名称
 * @param {*} value //cookie的值
 * @param {*} day //cookie的过期时间
 */
const setCookie = (name, value, day) => {
  if (day !== 0) {
    var expires = day * 24 * 60 * 60 * 1000 // 转化为秒
    var date = new Date(+new Date() + expires)
    document.cookie = name + '=' + value + ';expires=' + date.toUTCString()
  } else {
    document.cookie = name + '=' + escape(value)
  }
}

/**
 * @param {*} name //cookie的名称
 */
const getCookie = (name) => {
  var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)')
  var arr = document.cookie.match(reg)
  if (arr) {
    return unescape(arr[2])
  } else {
    return null
  }
}

/**
 * @param {*} name //删除cookie的值
 */
const delCookie = (name) => {
  setCookie(name, '', -1)
}

/**
 * @base64 // base64 解码转16进制
 * @num  需要获取16进制的数量
 */
const bese64_CodeAt = (base64, num) => {
  // let str = window.atob(file.src.split('data:application/octet-stream;base64,')[1])
  const str = window.atob(base64)
  var val = []
  for (let i = 0; i < str.length; i++) {
    var _hex = str.charCodeAt(i).toString(16)
    if (i < num) {
      val.push((_hex.length == 2 ? _hex : '0' + _hex))
    }
  }
  return val
}

/** 排序
 * @index 下标
 * @num  页数
 * @size  当前页数的条数
 */
const changSort = (index, num, size) => {
  index = index + 1
  if (num > 1) {
    index = index + (num - 1) * size
  }
  return index
}

/**
 * 获取国家
 */
const getCountryName = (data, id) => {
  let countryName = ''
  data.forEach(e => {
    if (e.countryId == id) {
      countryName = e.countryName
    }
  })
  return countryName
}

/**
 * 判断是否为海外环境
 * @returns {boolean}
 */
const isFormalEnvironment = () => {
  const url = window.location.href
  return url.indexOf('overseas') !== -1
}

/**
 * 判断是否为正式环境
 * @returns {boolean}
 */
const isTestEnvironment = () => {
  const url = window.location.href
  return url.indexOf('https://app.ss-tjd.com') !== -1 || url.indexOf('http://app.ss-tjd.com') !== -1 || url.indexOf('overseas') !== -1
}

export {
  getCookie,
  setCookie,
  delCookie,
  bese64_CodeAt,
  changSort,
  getCountryName,
  isFormalEnvironment,
  isTestEnvironment
}
